#!/bin/bash

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 获取项目根目录（当前目录的上三级目录，即backend目录）
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../../" && pwd)"
# 检查项目根目录是否存在
if [ ! -d "$PROJECT_ROOT" ]; then
    echo "Error: Project root directory not found"
    exit 1
fi
echo "Project root directory: $PROJECT_ROOT"
# 设置runtime目录路径为项目根目录下的runtime目录
RUNTIME_DIR="$PROJECT_ROOT/runtime"
# 进入脚本所在目录
cd "$SCRIPT_DIR" || exit 1

# 执行make命令
echo "Building lib_news.so..."c
make clean
if ! make; then
    echo "Error: Build failed"
    exit 1
fi

# 检查编译结果是否存在
if [ ! -f "lib_news.so" ]; then
    echo "Error: lib_news.so not found after build"
    exit 1
fi

# 创建runtime目录（如果不存在）
mkdir -p "$RUNTIME_DIR"

# 移动编译结果到runtime目录
echo "Moving lib_news.so to runtime directory..."
mv lib_news.so "$RUNTIME_DIR/"

echo "Build completed successfully. lib_news.so moved to $RUNTIME_DIR/"
make clean